package com.wodsy.phoenix.commons.utils.sms;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class SMSSenderUtil
{
    private String username;
    private String password;
    private int productid;
    private String xh;
    private String sendurl;
    private static SMSSenderUtil captcha = null;
    private static SMSSenderUtil notice = null;
    private static SMSSenderUtil marketing = null;

    static
    {
        captcha = new SMSSenderUtil();
        captcha.init(1);

        notice = new SMSSenderUtil();
        notice.init(2);

        marketing = new SMSSenderUtil();
        marketing.init(3);
    }

    public static String sendCaptcha(String mobile, String content)
    {
        return sendSms(mobile, content, 1);
    }

    public static String sendNotice(String mobile, String content)
    {
        return sendSms(mobile, content, 2);
    }

    public static String sendMarketing(String mobile, String content)
    {
        return sendSms(mobile, content, 3);
    }

    private static String sendSms(String mobile, String content, int type)
    {
        if ((mobile == null) || (mobile.equals("")))
        {
            System.out.println("SMSLOG：手机号为空");
            return "mobile";
        }
        SMSSenderUtil sms = null;
        switch (type)
        {
            case 1:
                sms = captcha;
                break;
            case 2:
                sms = notice;
                break;
            case 3:
                sms = marketing;
                break;
        }
        String sendUrl = null;
        try
        {
            sendUrl =sms.getSendurl().replace("#username#", sms.getUsername()).replace("#password#", sms.getPassword()).replace("#mobile#", mobile).replace("#content#", URLEncoder.encode(content, "UTF-8")).replace("#productid#", String.valueOf(sms.getProductid() == 0 ? "" : Integer.valueOf(sms.getProductid()))).replace("#xh#", sms.getXh());
        }
        catch (UnsupportedEncodingException uee)
        {
            System.out.println("SMSLOG：请求运营商异常:" + sendUrl);
        }
        return sendMsg(sendUrl);
    }


    public static String sendMsg(String urlString) {
        HttpGet httpRequest = new HttpGet(urlString);

        try
        {
            //取得HttpClient对象
            HttpClient httpclient = new DefaultHttpClient();
            //请求HttpClient，取得HttpResponse
            HttpResponse httpResponse = httpclient.execute(httpRequest);
            //请求成功
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                //取得返回的字符串
                String strResult = EntityUtils.toString(httpResponse.getEntity());
                return strResult;
            }
            else
            {
                return null;
            }
        }
        catch (ClientProtocolException e)
        {
            return null;
        }
        catch (IOException e)
        {
            return null;
        }
        catch (Exception e)
        {
            return null;
        }


    }

    private void init(int smstype)
    {
        int send = 0;
        switch (send)
        {
            case 0:
                if (smstype == 3)
                {
                    this.username = "xpt11796";
                    this.sendurl = "http://125.208.3.91:8888/sms.aspx?action=send&userid=#productid#&account=#username#&password=#password#&mobile=#mobile#&content=#content#&sendTime=&extno=#xh#";
                    this.productid = 7864;
                }
                else
                {
                    this.username = "xd000473";
                    this.sendurl = "http://dx.ipyy.net/sms.aspx?action=send&userid=#productid#&account=#username#&password=#password#&mobile=#mobile#&content=#content#&sendTime=&extno=#xh#";

                    this.productid = 0;
                }
                this.password = "Wodsy678";
                this.xh = "";
                break;
            case 1:
                this.username = "wodsy";
                this.password = "Wodsy678";
                this.xh = "";
                if (smstype == 1) {
                    this.productid = 676767;
                } else if (smstype == 2) {
                    this.productid = 676766;
                } else {
                    this.productid = 676768;
                }
                this.sendurl = "http://www.ztsms.cn:8800/sendSms.do?username=#username#&password=#password#&mobile=#mobile#&content=#content#&productid=#productid#&xh=#xh#";
                break;
        }
    }

    public String getUsername()
    {
        return this.username;
    }

    public String getPassword()
    {
        return this.password;
    }

    public int getProductid()
    {
        return this.productid;
    }

    public String getXh()
    {
        return this.xh;
    }

    public String getSendurl()
    {
        return this.sendurl;
    }
}
